package com.vhall.component.controller.account;

import com.vhall.component.controller.account.facade.WhiteAuthFacade;
import com.vhall.component.entity.account.vo.AuthLoginRspVO;
import com.vhall.component.entity.account.vo.AuthLoginVO;
import com.vhall.component.entity.account.vo.WhiteLoginVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.service.account.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author lsl
 * @since 2021/6/25
 */
@VhallComponent
public class WhiteAuthController implements WhiteAuthFacade {

    @Autowired
    private AccountService accountService;

    /**
     * 手机号判断
     */
    private static final Pattern PHONE = Pattern.compile("^1[3-9]{1}\\d{9}$");

    @Override
    public AuthLoginRspVO whiteLogin(WhiteLoginVO reqVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();

        String name = reqVO.getWhiteName();
        AuthLoginVO authLoginVO = new AuthLoginVO();
        authLoginVO.setRoomId(reqVO.getRoomId());
        authLoginVO.setNickname(name);
        Matcher match = PHONE.matcher(name);
        if (match.matches()) {
            authLoginVO.setPhone(name);
        } else {
            authLoginVO.setPhone("0");
        }
        authLoginVO.setPassword(reqVO.getWhitePaas());
        authLoginVO.setLoginType(reqVO.getLoginType());
        return accountService.authLogin(authLoginVO, request, response, false, name);
    }
}
